<?php

//	Author: 	Christos Polydorou
//	Email:		std04237@di.uoa.gr
//	Purpose: 	Search for an entity

require_once ("../DB/connection.php");
require_once ("../DB/defines.php");
require_once ("../Frameworks/Objects/user.php");
require_once ("../Frameworks/Objects/category.php");

function DB_searchEntities($userid, $entityname)
{

    global $conn;

    // The name of the stored procedure
    $sp_name = "usp_searchEntities";

    // The return variable
    $retval;

    // Create a new statement
    $stmt = mssql_init($sp_name, $conn);

    // Bind values
    mssql_bind($stmt, "RETVAL", $retval, SQLINT4, true, false, 1);
    mssql_bind($stmt, '@userid', $userid, SQLINT4, false, false, 1);
    mssql_bind($stmt, '@entityname', $entityname, SQLVARCHAR, false, false, 50);

    // Execute the statement
    $status = mssql_execute($stmt);

    if ($status)
    {
        $entities = array();
        $i = '0';
        while ($row = mssql_fetch_row($status))
        {
            $ent = new Entity();
            $ent->SetCategoryId($row[1]);
            $ent->SetName($row[2]);
            $ent->SetId($row[0]);
            $entities[$i] = $ent;
            $i++;
            mssql_next_result($status);
        }
        return $entities;
    }
}

?>